package com.whnk.web.controller.pm;

import java.text.ParseException;
import java.util.List;

import com.whnk.pm.dto.CheckDateDurationDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.whnk.common.annotation.Log;
import com.whnk.common.enums.BusinessType;
import com.whnk.pm.domain.BaseInfo;
import com.whnk.pm.domain.WorkInfo;
import com.whnk.pm.service.IBaseInfoService;
import com.whnk.pm.service.IWorkInfoService;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.common.base.AjaxResult;

/**
 * 工作经历 信息操作处理
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Controller
@RequestMapping("/pm/workInfo")
public class WorkInfoController extends BaseController {
	private String prefix = "pm/workInfo";

	@Autowired
	private IWorkInfoService workInfoService;

	@Autowired
	private IBaseInfoService baseInfoService;

	@RequiresPermissions("pm:workInfo:view")
	@GetMapping()
	public String workInfo() {
		return prefix + "/workInfo";
	}

	/**
	 * 查询工作经历列表
	 */
	@RequiresPermissions("pm:workInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WorkInfo workInfo) {
		startPage();
		List<WorkInfo> list = workInfoService.selectWorkInfoList(workInfo);
		return getDataTable(list);
	}

	/**
	 * 新增工作经历
	 */
	@GetMapping("/add/{empId}")
	public String add(@PathVariable("empId") String empId, ModelMap mmap) {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
		return prefix + "/add";
	}

	/**
	 * 新增保存工作经历
	 */
	@RequiresPermissions("pm:workInfo:add")
	@Log(title = "工作经历", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WorkInfo workInfo) {
		return toAjax(workInfoService.insertWorkInfo(workInfo));
	}

	/**
	 * 修改工作经历
	 */
	@GetMapping("/edit/{workId}")
	public String edit(@PathVariable("workId") Integer workId, ModelMap mmap) {
		WorkInfo workInfo = workInfoService.selectWorkInfoById(workId);
		mmap.put("workInfo", workInfo);
		return prefix + "/edit";
	}

	/**
	 * 修改保存工作经历
	 */
	@RequiresPermissions("pm:workInfo:edit")
	@Log(title = "工作经历", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WorkInfo workInfo) {
		return toAjax(workInfoService.updateWorkInfo(workInfo));
	}

	/**
	 * 删除工作经历
	 */
	@RequiresPermissions("pm:workInfo:remove")
	@Log(title = "工作经历", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(workInfoService.deleteWorkInfoByIds(ids));
	}

	/**
	 * 查看详细信息
	 * @param workId
	 * @param mmap
	 * @return
	 */
	@RequiresPermissions("pm:workInfo:detail")
	@GetMapping("/detail/{workId}")
	public String detail(@PathVariable("workId") Integer workId, ModelMap mmap) {
		WorkInfo workInfo = workInfoService.selectWorkInfoById(workId);
		mmap.put("workInfo", workInfo);
		return prefix + "/detail";
	}

	@PostMapping("/checkDate")
	@ResponseBody
	public String checkDuration(CheckDateDurationDTO checkDateDurationDTO) throws ParseException {
		return workInfoService.checkDate(checkDateDurationDTO.getStartDate(),checkDateDurationDTO.getEndDate(),checkDateDurationDTO.getEmpId(), checkDateDurationDTO.getOperationType(), checkDateDurationDTO.getId());
	}
}
